package org.hackystat.ambientdevice.trigger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.hackystat.ambientdevice.ConfigurationException;
import org.hackystat.ambientdevice.server.jaxb.Project;
import org.hackystat.ambientdevice.server.jaxb.Trigger;
import org.hackystat.dailyprojectdata.client.DailyProjectDataClient;
import org.hackystat.sensorbase.client.SensorBaseClient;
import org.junit.Before;
import org.junit.Test;

/**
 * Test class for Trigger Factory.
 * 
 * @author Shaoxuan Zhang
 */
public class TestTriggerFactory {
  /** Project for test. */
  private Project project;
  /** Trigger factory for test. */
  private TriggerFactory triggerFactory;
  
  /**
   * Prepare the factory and project for tests.
   */
  @Before
  public void setUp() {
    DailyProjectDataClient dpdClient = new DailyProjectDataClient("TestHost", "test", "test");
    SensorBaseClient sensorBaseClient = new SensorBaseClient("TestHost", "test", "test");
    triggerFactory = new TriggerFactory(sensorBaseClient, dpdClient);

    project = new Project();
    project.setOwner("test");
    project.setProjectName("testProject");
  }
  
  /**
   * Test case to create a sensorData trigger.
   * @throws ConfigurationException when triggers are not valid.
   */
  @Test
  public void testCreateSensorDataTrigger() throws ConfigurationException {

    Trigger trigger = new Trigger();
    trigger.setType("SensorData");
    trigger.setId("testBuild");
    trigger.setSDT("Build");
    trigger.setProperty("Result");
    trigger.setValue("Success");

    HackystatTrigger buildTrigger = triggerFactory.getTrigger(project, trigger);
    assertTrue("Should be an instance of SensorDataTrigger",
        buildTrigger instanceof SensorDataTrigger);
    assertEquals("Checking project", project, buildTrigger.getProject());
  }

  /**
   * Test case to create a coverage trigger.
   * @throws ConfigurationException when triggers are not valid.
   */
  @Test
  public void testCreateCoverageTrigger() throws ConfigurationException {
    Trigger trigger = new Trigger();
    trigger.setType("Coverage");
    trigger.setId("testCoverage");
    trigger.setProperty("Compare");
    trigger.setMaxValue(90.0);
    trigger.setMinValue(40.0);
    
    HackystatTrigger coverageTrigger = triggerFactory.getTrigger(project, trigger);
    assertTrue("Should be an instance of SensorDataTrigger",
        coverageTrigger instanceof CoverageTrigger);
    assertEquals("Checking project", project, coverageTrigger.getProject());
  }
}
